package nl.boelen.game.graphics;

import nl.boelen.game.Game;

public class Screen {
    public static final int BORDER = 2;

    private int xOffset = -BORDER;
    private int yOffset = -BORDER;
    public int bottom_area = 40 + BORDER;
    public int top_area = 0;
    public int left_area = 0;
    public int right_area = 0;

    public int width;
    public int height;

    private SpriteSheet spriteSheet;
    public int[] pixels;

    public Screen(int w, int h, SpriteSheet spriteSheet) {
        this.width = w;
        this.height = h;
        this.spriteSheet = spriteSheet;

        pixels = new int[w * h];
    }

    public void renderTile(int xPos, int yPos, int tile) {
        renderTile(xPos, yPos, tile, false, false, false);
    }

    public void renderTile(int xPos, int yPos, int tile, boolean hasAlpha) {
        renderTile(xPos, yPos, tile, false, false, hasAlpha);
    }

    public void renderTile(int xPos, int yPos, int tile, boolean mirrorX, boolean mirrorY, boolean useAlpha) {
        xPos -= xOffset;
        yPos -= yOffset;

        int xTile = tile % 16;
        int yTile = tile / 16;
        int tileOffset = (xTile * Game.TILE_SIZE) + (yTile * Game.TILE_SIZE) * spriteSheet.width;

        for (int y = 0; y < Game.TILE_SIZE; y++) {
            if (y + yPos < 0 + (top_area + BORDER) || y + yPos >= height - (bottom_area + BORDER)) {
                continue;
            }
            int ySheet = y;
            if (mirrorY) {
                ySheet = Game.TILE_SIZE - 1 - y;
            }

            for (int x = 0; x < Game.TILE_SIZE; x++) {
                if (x + xPos < 0 + (left_area + BORDER) || x + xPos >= width - (right_area + BORDER)) {
                    continue;
                }
                int xSheet = x;
                if (mirrorX) {
                    xSheet = Game.TILE_SIZE - 1 - x;
                }

                int pixelColor = spriteSheet.pixels[tileOffset + xSheet + ySheet * spriteSheet.width];
                if (!useAlpha || useAlpha && pixelColor != Color.INVISIBLE.getColor()) {
                    pixels[(x + xPos) + (y + yPos) * width] = pixelColor;
                }
            }
        }
    }

    public void renderBuildMenu(Color background) {
        for (int y = height - BORDER - bottom_area; y < height; y++) {
            if (y > height - BORDER) {
                continue;
            }
            for (int x = BORDER; x < width; x++) {
                if (x > width - BORDER) {
                    continue;
                }
                if (y >= height - bottom_area - BORDER * 2 && y <= height - bottom_area - BORDER) {
                    pixels[x + y * width] = Color.BLACK.getColor();
                }else{
                    pixels[x + y * width] = background.getColor();
                }
            }
        }
    }

    public void setOffset(int x, int y) {
        xOffset = x;
        yOffset = y;
    }

    public void addOffset(int x, int y) {
        xOffset += x;
        yOffset += y;
    }

    public int getOffsetX() {
        return xOffset;
    }

    public int getOffsetY() {
        return yOffset;
    }

    public void changeZoom(boolean zoomIn) {
        // if (zoomIn) {
        // height -= 10;
        // width -= 10;
        // pixels = new int[width * height];
        // System.out.println("Zoom in");
        // } else {
        // height += 10;
        // width += 10;
        // pixels = new int[width * height];
        // System.out.println("Zoom out");
        // }
    }
}
